package model

import (
	"fmt"

	"gorm.io/gorm"
)

type AdoptComment struct {
	AdoptID         int `gorm:"not null" `
	PromoterID      int `gorm:"not null" `
	ApplyID         int `gorm:"not null" `
	PromoterScore   int
	ApplyScore      int
	PromoterComment string
	ApplyComment    string
	Adopt           *Adopt         `gorm:"foreignKey:AdoptID"`
	DeletedAt       gorm.DeletedAt `gorm:"index"`
}

type AdoptCommentMini struct {
	Score   int
	Comment string
}

func GetAdoptCommentMiniByID(AdoptID int, UserID int) (AdoptCommentMini, error) {
	var err error
	var comment AdoptComment

	err = DB.Model(AdoptComment{}).Where("AdoptID = ?", AdoptID).Find(&comment).Error
	if err != nil {
		return AdoptCommentMini{}, err
	}

	if UserID == comment.ApplyID {
		return AdoptCommentMini{
			Score:   comment.ApplyScore,
			Comment: comment.ApplyComment,
		}, nil
	} else if UserID == comment.PromoterID {
		return AdoptCommentMini{
			Score:   comment.PromoterScore,
			Comment: comment.PromoterComment,
		}, nil
	}
	return AdoptCommentMini{}, nil
}

func (ac *AdoptComment) Create() (*AdoptComment, error) {
	err := DB.Create(ac).Error
	if err != nil {
		return nil, err
	}
	return ac, nil
}

// 根据申请表的ID查看对应的评论是否已经存在
func SearchComment(AdoptID int) (AdoptComment, error) {
	var err error
	var comment AdoptComment
	err = DB.Model(AdoptComment{}).Where("AdoptID = ?", AdoptID).First(&comment).Error
	if err != nil {
		return AdoptComment{}, err
	}
	return comment, nil
}

func (ac *AdoptComment) UpdateComments() (*AdoptComment, error) {
	err := DB.Save(ac).Error
	if err != nil {
		return nil, err
	}
	return ac, nil
}

// 更新已存在的评价信息
func (ac *AdoptComment) SetComments() (*AdoptComment, error) {
	err := DB.Model(AdoptComment{}).Where("AdoptID = ?", ac.AdoptID).Updates(AdoptComment{
		PromoterScore:   ac.PromoterScore,
		ApplyScore:      ac.ApplyScore,
		PromoterComment: ac.PromoterComment,
		ApplyComment:    ac.ApplyComment,
		PromoterID:      ac.PromoterID,
		ApplyID:         ac.ApplyID,
	}).Error
	if err != nil {
		return nil, err
	}
	return ac, nil
}

type UserElavuation struct {
	UID      int    `json:"uid"`
	UName    string `json:"uname"`
	UAvatar  string `json:"uavatar"`
	UComment string `json:"ucomment"`
	UScore   int    `json:"uscore"`
}

// 根据用户的ID查询有关他的评价的所有得分
func SearchScoreByUserID(UserID int) ([]int, []UserElavuation, []UserElavuation, error) {
	var err error
	//查询作为申请者的评价
	var applyScore []int
	var comm1 []AdoptComment
	err = DB.Model(AdoptComment{}).Where("ApplyID = ? ", UserID).Pluck("PromoterScore", &applyScore).Error
	err = DB.Model(AdoptComment{}).Where("ApplyID = ?", UserID).Find(&comm1).Error
	if err != nil {
		fmt.Println("作为申请者评价信息为空！")
	}
	var applyUser []UserElavuation
	for _, v := range comm1 {
		user, err := GetUserMiniByID(v.PromoterID)
		if err != nil {
			fmt.Println("查询用户信息失败！")
		}
		applyUser = append(applyUser, UserElavuation{
			UID:      user.UserID,
			UName:    user.UName,
			UAvatar:  user.ProfilePhoto,
			UComment: v.PromoterComment,
			UScore:   v.PromoterScore,
		})
	}

	//查询作为发起者的评价得分
	var promoterScore []int
	var comm2 []AdoptComment
	var promoterUser []UserElavuation
	err = DB.Model(AdoptComment{}).Where("PromoterID = ?", UserID).Pluck("ApplyScore", &promoterScore).Error
	err = DB.Model(AdoptComment{}).Where("PromoterID = ?", UserID).Find(&comm2).Error
	if err != nil {
		fmt.Println("作为发起者评价信息为空！")
	}
	for _, v := range comm2 {
		user, err := GetUserMiniByID(v.ApplyID)
		if err != nil {
			fmt.Println("查询用户信息失败！")
		}
		promoterUser = append(promoterUser, UserElavuation{
			UID:      user.UserID,
			UName:    user.UName,
			UAvatar:  user.ProfilePhoto,
			UComment: v.ApplyComment,
			UScore:   v.ApplyScore,
		})
	}
	return append(applyScore, promoterScore...), applyUser, promoterUser, nil
}
